// pages/detail/detail.js
const db=wx.cloud.database({});
const item=db.collection('Course');

Page({
  data: {
    //课程的id
    itemId:'',
    imageData:[],
    itemData:[],
    brief:'',
    price:'',
    startTime:0,
    week:0,
    length:0,
    coach:'',
    place:'',
    openid:'',
    courseSalesData:[],
    cnt1:'1',
    cnt:'1'
  },

   onLoad: function (options) {
    
    var that=this
    //接受商品的ID
    that.setData({
      itemId:options
    })

    db.collection('Course').where({
      _id:this.data.itemId.itemId
    }).get({
      success:function(res){
        console.log(res.data)

        that.setData({
          imageData:res.data[0].images,
          itemData:res.data,
          brief:res.data[0].brief,
          price:res.data[0].price,
          week:res.data[0].week,
          startTime:res.data[0].startTime,
          length:res.data[0].length,
          coach:res.data[0].coach,
          place:res.data[0].place
        })
      }
    })

    

    wx.getUserInfo({
      success: res => {
        that.setData({
           userInfo:res.userInfo,
        }),
        console.log(res.userInfo)
        wx.cloud.callFunction({
        //调用云函数获取openid
        name: "login",
        success: res => {
         that.setData({
          openid:res.result.openid
         }),

         

         console.log(res.result)

         db.collection('CourseSales').where({
          _openid:this.data.openid
        }).get({
            success:res=>{
              //对所有已购买的课程时间和星期
              for(var i=0;i<res.data.length;i++){
    
              db.collection('Course').where({
                _id:res.data[i].courseId
              }).get({
                success:re=>{
                  console.log(re)
                  if(re.data[0].week==this.data.week&&re.data[0].startTime==this.data.startTime){
                    this.setData({
                      cnt:'0'
                    })
                  }
                }
              })
            }
          }
        })
         //进行初始化操作
        //  that.data.userInfo.openid = that.data.openid,
        //  wx.setStorageSyneSync("userInfo", that.data.userInerInfo)
        },
        fail: res => {
          console.log(res)
          return;
       }
      })
    },
      fail: res => {
        console.log(res)
        return
      }
    })
 },

  handleBuyCourse(){
    var that=this
    //1.判断是否是会员
    db.collection('VIPSales').where({
      _openid:that.data.openid
    }).get({
      success:res=>{
        console.log(res)
        if(res.data.length==0){
          that.setData({
            cnt1:'0'
          })
          wx.showModal({
            cancelColor: 'red',
            title:'是否购买会员',
            content:'必须是会员才能购买课程',
            success:res=>{
              console.log(res)

              if(res.cancel){
                console.log('点击了取消')
              }
              //在这里跳转到会员购买的页面
              if(res.confirm){
                wx.navigateTo({
                  url: "/pages/vipBuying/vipBuying?openid=" + this.data.openid,
                })
              }
            }
          })
        }

      //判断购买的课程是否与已有时间冲突
      if(this.data.cnt1=='1'&&this.data.cnt=='0'){
        wx.showModal({
          cancelColor: 'red',
          title:'购买失败',
          content:'与现有上课时间冲突',
          showCancel:false,
          success:function(res){
          console.log(res)
          }
        })
      }
      // db.collection('CourseSales').where({
      //   _openid:this.data.openid
      // }).get({
      //     success:res=>{
      //       //对所有已购买的课程时间和星期
      //       for(var i=0;i<res.data.length;i++){

      //       db.collection('Course').where({
      //         _id:res.data[i].courseId
      //       }).get({
      //         success:re=>{
      //           console.log(re)
      //           if(re.data[0].week==this.data.week&&re.data[0].startTime==this.data.startTime){
      //             this.setData({
      //               cnt:'1'
      //             })
      //             if(that.data.cnt=='0'){
                  
      //             this.setData({
      //               cnt1:'1'
      //             })
      //             wx.showModal({
      //               cancelColor: 'red',
      //               title:'购买失败',
      //               content:'与现有上课时间冲突',
      //               showCancel:false,
      //               success:function(res){
      //                 console.log(res)
      //               }
      //             })
      //             }
      //           }

                //已确定是会员以及课程没有重复(cnt==0)，往CourseSales添加记录
              if(that.data.cnt=='1'&&that.data.cnt1=='1'){
              db.collection('CourseSales').add({
                data:{
                  _openid:this.data.openid,
                  studentID:this.data.openid,
                  courseId:this.data.itemId.itemId,
                  times:10,
                  remainingTimes:10,
                  sign:[],
                  status:3
                },
                success:function(res){
                  console.log(res)
                  wx.showToast({
                    title: '购买成功',
                  })
                }
              })
  
              // db.collection('Record').add({
              //   data:{
              //     studentID:this.data.openid,
              //     signTime:[]
              //   },
              //   success:function(res){
              //     console.log(res)
                  
              //   }
              // })
              }


        //       }
        //     })
        //   }

            
        //   }

          
        // })
      }
    })
  }
})